package com.huanglj.taobaounion.ui.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.huanglj.taobaounion.R;
import com.huanglj.taobaounion.base.BaseActivity;
import com.huanglj.taobaounion.base.BaseFragment;
import com.huanglj.taobaounion.ui.fragment.HomeFragment;
import com.huanglj.taobaounion.ui.fragment.RedPacketFragment;
import com.huanglj.taobaounion.ui.fragment.SearchFragment;
import com.huanglj.taobaounion.ui.fragment.SelectedFragment;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MainActivity extends BaseActivity {
    private static final String TAG = "MainActivity";

    private FrameLayout mMainPage;
    @BindView(R.id.main_navigation_bar) BottomNavigationView mNavigationView;
    private HomeFragment mHomeFragment;
    private FragmentTransaction mTransaction;
    private RedPacketFragment mRedPacket;
    private SearchFragment mSearchPacket;
    private SelectedFragment mSelectedPacket;
    private FragmentManager mSupportFragmentManager;
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }


    protected void initFragment() {
        mHomeFragment = new HomeFragment();
        mRedPacket = new RedPacketFragment();
        mSearchPacket = new SearchFragment();
        mSelectedPacket = new SelectedFragment();
        mSupportFragmentManager = getSupportFragmentManager();
        switchFragment(mHomeFragment);
    }

    protected void initEvent() {
        mNavigationView.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()){
                case R.id.home:
                    switchFragment(mHomeFragment);
                    break;
                case R.id.red_packet:
                    switchFragment(mRedPacket);
                    break;
                case R.id.select:
                    switchFragment(mSelectedPacket);
                    break;
                case R.id.search:
                    switchFragment(mSearchPacket);
                    break;
            }
            return true;
        });
    }

    private static BaseFragment sLastFragment = null;
    protected void switchFragment(@org.jetbrains.annotations.NotNull BaseFragment fragment) {
        mTransaction = mSupportFragmentManager.beginTransaction();
        if (!fragment.isAdded()) {
            mTransaction.add(R.id.main_page_container,fragment);
        }
        if (sLastFragment != null) {
            mTransaction.hide(sLastFragment);
        }
        mTransaction.show(fragment);
        sLastFragment = fragment;
        mTransaction.commit();
    }

    protected void initView() {


    }
}
